﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMTC.Model.Model;
using SMTC.Model.Domain;
using System.Web.UI.HtmlControls;
using SMTC.BO.AnalyzePara;
using SMTC.WebCommon.Common;

public partial class Themes_Home : BasePage
{
    ReportDataSource datasource = new ReportDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void OnLoadComplete(EventArgs e)
    {
        string[] str = dll_PlanVersion.Text.Split('-');
        if (str != null && str.Length > 1)
        {
            CapacityDataSource.SelectParameters["start"].DefaultValue = str[0].Substring(0, 4) + "-01-01";
            CapacityDataSource.SelectParameters["end"].DefaultValue = str[0].Substring(0, 4) + "-12-31";
        }

        if (!IsPostBack)
        {
            TeeePDataSource();
        }
        base.OnLoadComplete(e);
    }

    protected void lbtn_Query_Click(object sender, EventArgs e)
    {
        TeeePDataSource();
        rep_Capacity.DataBind();
        rep_Plan.DataBind();
    }

    public void TeeePDataSource()
    {
        string[] str = dll_PlanVersion.Text.Split('-');
        string start = "1900-1-1", end = "2999-12-31";
        if (str != null && str.Length > 1)
        {
            start = str[0].Substring(0, 4) + "-01-01";
            end = str[0].Substring(0, 4) + "-12-31";
        }

        List<ImportPlanDomain> planList = datasource.GetReportPlan(this.dll_PlanVersion.Text);
        List<ImportPlanDomain> capacity = datasource.GetReportCapacity(DateTime.Parse(start), DateTime.Parse(end), dll_PUVersion.Text);
        List<ImportPlanDomain> result = new List<ImportPlanDomain>();

        foreach (var planItem in planList)
        {
            ImportPlanDomain resultItem = new ImportPlanDomain();
            try
            {
                ImportPlanDomain capacityItem = capacity.Single(item => item.BU == planItem.BU);
                resultItem.BU = planItem.BU;
                resultItem.p1 = (float)planItem.m1 / capacityItem.m1 * 100;
                resultItem.p2 = (float)planItem.m2 / capacityItem.m2 * 100;
                resultItem.p3 = (float)planItem.m3 / capacityItem.m3 * 100;
                resultItem.p4 = (float)planItem.m4 / capacityItem.m4 * 100;
                resultItem.p5 = (float)planItem.m5 / capacityItem.m5 * 100;
                resultItem.p6 = (float)planItem.m6 / capacityItem.m6 * 100;
                resultItem.p7 = (float)planItem.m7 / capacityItem.m7 * 100;
                resultItem.p8 = (float)planItem.m8 / capacityItem.m8 * 100;
                resultItem.p9 = (float)planItem.m9 / capacityItem.m9 * 100;
                resultItem.p10 = (float)planItem.m10 / capacityItem.m10 * 100;
                resultItem.p11 = (float)planItem.m11 / capacityItem.m11 * 100;
                resultItem.p12 = (float)planItem.m12 / capacityItem.m12 * 100;
                result.Add(resultItem);
            }
            catch
            {
                ImportPlanDomain capacityExcprionItem = new ImportPlanDomain();
                capacityExcprionItem.BU = planItem.BU;
                result.Add(capacityExcprionItem);
            }
        }

        rep_Teeep.DataSource = result;
        rep_Teeep.DataBind();
    }

    protected void rep_Teeep_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        ImportPlanDomain _ImportPlanDomain = e.Item.DataItem as ImportPlanDomain;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            for (int index = 1; index <= 12; index++)
            {
                if (this.GetPFormDomain(_ImportPlanDomain, index) >= (float)Para.YellowAlert
                    && this.GetPFormDomain(_ImportPlanDomain, index) < (float)Para.RedAlert)
                {
                    this.SetCellColor(e.Item.FindControl("tdp" + index) as HtmlTableCell, 2);
                }
                else if (this.GetPFormDomain(_ImportPlanDomain, index) >= (float)Para.RedAlert)
                {
                    this.SetCellColor(e.Item.FindControl("tdp" + index) as HtmlTableCell, 1);
                }
            }

        }
    }

    public void SetCellColor(HtmlTableCell cell, int type)
    {
        if (type == 1)
        {
            cell.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Red");
            cell.Style.Add(HtmlTextWriterStyle.Color, "White");
            cell.Style.Add(HtmlTextWriterStyle.FontWeight, "bolder");
        }
        else if (type == 2)
        {
            cell.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Yellow");
            cell.Style.Add(HtmlTextWriterStyle.Color, "black");
            cell.Style.Add(HtmlTextWriterStyle.FontWeight, "bolder");
        }

    }

    public float GetPFormDomain(ImportPlanDomain domain, int index)
    {
        switch (index)
        {
            case 1: return domain.p1;
            case 2: return domain.p2;
            case 3: return domain.p3;
            case 4: return domain.p4;
            case 5: return domain.p5;
            case 6: return domain.p6;
            case 7: return domain.p7;
            case 8: return domain.p8;
            case 9: return domain.p9;
            case 10: return domain.p10;
            case 11: return domain.p11;
            case 12: return domain.p12;
            default: return 0;
        }
    }


    public SMT_paraModel Para
    {
        get
        {
            AnalyzeParaQueryServices service = new AnalyzeParaQueryServices();
            if (ViewState["Para"] != null)
            {
                return ViewState["Para"] as SMT_paraModel;
            }
            else
            {
                ViewState["Para"] = service.getPara();
                return ViewState["Para"] as SMT_paraModel;
            }
        }

    }
}

